package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by peterhu on 2017/4/13.
 */

public class GridLayerView extends View implements View.OnClickListener {

    private Canvas canva;

    public GridLayerView(Context context) {
        super(context);
    }


    public GridLayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int kdefaultVertiMinValue = UIUtils.dp2px(80);

        int kdefaultHoriMinValue = UIUtils.dp2px(60);
        Paint mPaint = new Paint();
        mPaint.setColor(Color.CYAN);//设置画笔颜色为黑色
        mPaint.setStyle(Paint.Style.STROKE);//设置画笔style实心
        mPaint.setStrokeWidth(2);


        int verticalLineCount = getWidth() / kdefaultVertiMinValue;
        verticalLineCount = (verticalLineCount % 2 == 0) ? verticalLineCount : (verticalLineCount - 1);
        int vertiPadding = 1;
        if (verticalLineCount == 0) {
            vertiPadding = 0;
        } else {
            vertiPadding = getWidth() / verticalLineCount;
        }

        int horizonLineCount = getHeight() / kdefaultHoriMinValue;
        horizonLineCount = (horizonLineCount % 2 == 0) ? horizonLineCount : (horizonLineCount - 1);
        int horiPadding = 1;
        if (horizonLineCount == 0) {
            horiPadding = 0;
        } else {
            horiPadding = getHeight() / horizonLineCount;
        }


        Path shiziPath = new Path();
        shiziPath.moveTo(0, getHeight() / 2);
        shiziPath.lineTo(getWidth(), getHeight() / 2);
        shiziPath.moveTo(getWidth() / 2, 0);
        shiziPath.lineTo(getWidth() / 2, getHeight());
        canvas.drawPath(shiziPath, mPaint);


        Path oddPath = new Path();
        int odd_v_LineCount = verticalLineCount / 4;
        int odd_H_LineCount = horizonLineCount / 4;
        if (verticalLineCount / 2 % 2 == 0) {
            odd_v_LineCount--;
        }
        if (horizonLineCount / 2 % 2 == 0) {
            odd_H_LineCount--;
        }
        for (int i = 1; i < odd_v_LineCount + 1; i++) {
            oddPath.moveTo(getWidth() / 2 + 2 * i * vertiPadding, 0);
            oddPath.lineTo(getWidth() / 2 + 2 * i * vertiPadding, getHeight());
            oddPath.moveTo(getWidth() / 2 - 2 * i * vertiPadding, 0);
            oddPath.lineTo(getWidth() / 2 - 2 * i * vertiPadding, getHeight());
        }
        for (int i = 1; i < odd_H_LineCount + 1; i++) {
            oddPath.moveTo(0, getHeight() / 2 + 2 * i * horiPadding);
            oddPath.lineTo(getWidth(), getHeight() / 2 + 2 * i * horiPadding);
            oddPath.moveTo(0, getHeight() / 2 - 2 * i * horiPadding);
            oddPath.lineTo(getWidth(), getHeight() / 2 - 2 * i * horiPadding);
        }
        canvas.drawPath(oddPath, mPaint);

//虚线
        mPaint.setPathEffect(new DashPathEffect(new float[]{10, 5}, 0));
        Path evenPath = new Path();
        int even_v_LineCount = 1 + (verticalLineCount / 2 - 1) / 2;
        int even_h_LineCount = 1 + (horizonLineCount / 2 - 1) / 2;
        if (verticalLineCount / 2 % 2 == 1) {
            even_v_LineCount--;
        }
        if (horizonLineCount / 2 % 2 == 1) {
            even_h_LineCount--;
        }
        for (int i = 1; i < even_h_LineCount + 1; i++) {
            evenPath.moveTo(0, getHeight() / 2 + 2 * i * horiPadding - horiPadding);
            evenPath.lineTo(getWidth(), getHeight() / 2 + 2 * i * horiPadding - horiPadding);
            evenPath.moveTo(0, getHeight() / 2 - 2 * i * horiPadding + horiPadding);
            evenPath.lineTo(getWidth(), getHeight() / 2 - 2 * i * horiPadding + horiPadding);
        }
        for (int i = 1; i < even_v_LineCount + 1; i++) {
            evenPath.moveTo(getWidth() / 2 + 2 * i * vertiPadding - vertiPadding, 0);
            evenPath.lineTo(getWidth() / 2 + 2 * i * vertiPadding - vertiPadding, getHeight());
            evenPath.moveTo(getWidth() / 2 - 2 * i * vertiPadding + vertiPadding, 0);
            evenPath.lineTo(getWidth() / 2 - 2 * i * vertiPadding + vertiPadding, getHeight());
        }
        canvas.drawPath(evenPath, mPaint);
    }

    @Override
    public void onClick(View v) {

    }
}
